package com.chb.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chihb
 * @create-time 2024/10/23 20:42
 * @description
 */
public class Menu extends MenuComponent{
    private List<MenuComponent> menuComponents;

    public Menu(String name, int level){
        this.name = name;
        this.level = level;
        menuComponents = new ArrayList<>();
    }

    @Override
    void add(MenuComponent menuComponent) {
        menuComponents.add(menuComponent);
    }

    @Override
    void remove(MenuComponent menuComponent) {
        menuComponents.remove(menuComponent);
    }

    @Override
    MenuComponent getChild(int index) {
        return menuComponents.get(index);
    }

    @Override
    void print() {
        for (int i = 1; i < level; i++) {
            System.out.print("--");
        }
        System.out.println(name);
        for (MenuComponent menuComponent : menuComponents) {
            menuComponent.print();
        }
    }
}
